package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.personnel.api.dto.response.resp.PersonDetailResp;
import com.smedi.ismedi.progress.core.domain.dto.PersonQuery;
import com.smedi.ismedi.progress.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.service.PersonRemoteService;
import com.smedi.ismedi.personnel.api.dto.request.PersonListQueryRequest;
import com.smedi.ismedi.personnel.api.dto.response.resp.PersonResp;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class PersonRemoteServiceImpl implements PersonRemoteService {
    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;

    private final AdapterConverter adapterConverter;

    @Override
    public PersonResp getPersonOne(PersonQuery personQuery) {
        return null;
    }

    @Override
    public List<PersonResp> getPersonList(PersonQuery personQuery) {
        PersonListQueryRequest personListQueryRequest = adapterConverter.personQueryToPersonListQueryRequest(personQuery);
        ResponseData<List<PersonResp>> responseData = remotePersonCoreFeignApi.getPersonList(personListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }

    @Override
    public List<PersonDetailResp> getPersonDetailList(PersonQuery personQuery) {
        PersonListQueryRequest personListQueryRequest = adapterConverter.personQueryToPersonListQueryRequest(personQuery);
        ResponseData<List<PersonDetailResp>> responseData = remotePersonCoreFeignApi.getPersonDetailList(personListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }
}
